/*
* 在Info.plist添加一对关键字来许可位置服务.
* NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription
* 两个关键字对应的字符串是提示给用户看的,一般只要添加一个关键字即可
* 因为有AlwaysUsage那么WhenInUse情况下也能定位,反之则不能适用.
*/

//1.在代码中请求许可.
if locationManager.respondsToSelector(Selector("requestWhenInUseAuthorization")) {  
    locationManager.requestWhenInUseAuthorization()  
}  
//或者  
if locationManager.respondsToSelector(Selector("requestAlwaysAuthorization")) {  
    locationManager.requestAlwaysAuthorization()  
}

//2.如果最开始的时候设置的是WhenInUse, 而后需要使用Always,那么就需要在Plist和代码中更改状态
if locationManager.respondsToSelector(Selector("requestAlwaysAuthorization")) {
    requestAlwaysAuthorization()
}

func requestAlwaysAuthorization() {
    let status = CLLocationManager.authorizationStatus()
    if status == .Denied || status == .AuthorizedWhenInUse {
        let title = (status == .Denied) ? "定位是关闭状态" : "后台定位没有开启"
        let message = "需要开启Alway的定位服务"
        UIAlertView(title: title, message: message, delegate:self, cancelButtonTitle: "取消", otherButtonTitles: "设置")
    } else {
        locationManager.requestAlwaysAuthorization()
    }
}

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
    if buttonIndex == 1 {
        UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
    }
}